1. 题目描述(简单难度)

[warning] 剑指 Offer 59 - I. 滑动窗口的最大值

2. 解法一:双指针滑动窗口

class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
     if(nums.length == 0 || nums == null){
         return new int[0];
     }   
     int i = 0;
     int j = k-1;
     int n = nums.length-1;
     List<Integer> ans = new ArrayList<>();
     while(j<=n){
        int max = Integer.MIN_VALUE;
        for(int l=i;l<=j;l++){
           max = Math.max(max,nums[l]);
        }
        ans.add(max);
        i++;
        j++;
     }
     int[] resp = new int[ans.size()];
     for(int m=0;m<ans.size();m++){
        resp[m] = ans.get(m);
     }
     return resp;
    }
}

3. 解法二:优先队列

© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""